﻿singleApp.controller('categoriesCtrl', ['$http', '$scope', '$sce', 'FileUpload', '$timeout', function ($http, $scope, $sce, FileUpload, $timeout) {
    $scope.list = [];

    $scope.init = function () {
        layer.load(2);

        $http.get(window.singleConfig.rootUrl + "api/admin/CategoryService/getAll").then(function (result) {
            $scope.list = result.data.data;
            layer.closeAll();
        }, function () { });
    };

    $scope.init();

    $scope.model = { id: 0, order_num: "0", name: "" };
    $scope.modify = function (item) {
        if (item == null) {
            $scope.model = { id: 0, order_num: "100", name: "" };
        }
        else {
            $scope.model = { id: item.id, order_num: item.order_num, name: item.name };
        }
        $("#divModify").modal("show");
    };

    $scope.submit = function () {
        if ($scope.model.name == "") {
            layer.msg("名称不能为空！");
            return;
        }

        $http({
            method: 'POST',
            url: window.singleConfig.rootUrl + "api/admin/CategoryService/" + ($scope.model.id == 0 ? "add" : "edit"),
            data: $.param($scope.model),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
        .then(function (result) {
            $("#divModify").modal("hide");
            layer.msg("提交成功");
            $scope.init();
        }, function (data) {
            layer.closeAll();
            layer.msg("系统发生故障，请联系管理员");
        });
    };

    $scope.delete = function (item) {
        layer.confirm('确认删除吗？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $http.get(window.singleConfig.rootUrl + "api/admin/CategoryService/delete?id=" + item.id).then(function (result) {
                layer.closeAll();
                layer.msg("操作成功");
                $timeout(function () {
                    $scope.init();
                }, 1000);
            }, function () { });
        }, function () {
        });
    };
}]);